package com.zt.rongda.salary.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zt.rongda.constant.BusConstant;
import com.zt.rongda.employee.model.Employee;
import com.zt.rongda.employee.service.EmployeeService;
import com.zt.rongda.group.service.EmployeeGroupService;
import com.zt.rongda.order.model.OrderItem;
import com.zt.rongda.order.service.OrderService;
import com.zt.rongda.quota.model.QuotaConfig;
import com.zt.rongda.quota.service.QuotaConfigService;
import com.zt.rongda.salary.inter.SalaryPayRollDetailMapper;
import com.zt.rongda.salary.model.SalaryEmpBasic;
import com.zt.rongda.salary.model.SalaryEmpQuota;
import com.zt.rongda.salary.model.SalaryPayRoll;
import com.zt.rongda.salary.model.SalaryPayRollDetail;
import com.zt.rongda.salary.model.WorkBillItemQuotaDetail;
import com.zt.rongda.salary.model.WorkBillItemQuotaDetail.EmpQuotaDetail;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class SalaryPayRollDetailService extends BaseService {

	@Autowired
	private SalaryPayRollDetailMapper salaryPayRollDetailMapper;
	@Autowired
	private SalaryEmpBasicService salaryEmpBasicService;
	@Autowired
	private SalaryEmpQuotaService salaryEmpQuotaService;
	@Autowired
	private EmployeeGroupService employeeGroupService;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private QuotaConfigService quotaConfigService;
	@Autowired
	private SalaryPayRollService salaryPayRollService;

	/**
	 * 查询
	 *
	 * @param reqBean
	 * @return
	 */
	public JSONObject getTable(ReqBean reqBean) {
		SqlBuilder sqlBuilder = new SqlBuilder("");
		sqlBuilder.appendSql("select sp.PAYROLL_YEAR,sp.PAYROLL_MONTH,sp.status as payRollStatus,spd.* from SALARY_PAYROLL_DETAIL spd ");
		sqlBuilder.appendSql(" LEFT JOIN SALARY_PAYROLL sp  on sp.PAYROLL_ID=spd.PAYROLL_ID where 1=1");
		sqlBuilder.addWhere("sp.PAYROLL_YEAR", reqBean.getInfo("payRollYear"));
		sqlBuilder.addWhere("sp.PAYROLL_MONTH", reqBean.getInfo("payRollMonth"));
		sqlBuilder.addWhereLike2("spd.EMP_NAME", reqBean.getInfo("empName"));
		sqlBuilder.orderBy("order by sp.PAYROLL_YEAR desc ,sp.PAYROLL_MONTH desc");
		return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
	}

	/**
	 * 通过id查询
	 *
	 * @param paySlipId
	 * @return
	 */
	public SalaryPayRollDetail getById(String paySlipId) {
		return salaryPayRollDetailMapper.selectByPrimaryKey(paySlipId);
	}

	/**
	 * 新增
	 *
	 * @param salaryPayRollDetail
	 */
	public void add(SalaryPayRollDetail salaryPayRollDetail) {
		salaryPayRollDetail.setPaySlipId(stringId());// 手动设置Id

		salaryPayRollDetail.setMakeTime(new Date());
		salaryPayRollDetail.setModifyTime(salaryPayRollDetail.getMakeTime());
		salaryPayRollDetailMapper.insert(salaryPayRollDetail);
	}

	/**
	 * 修改
	 *
	 * @param salaryPayRollDetail
	 */
	public void update(SalaryPayRollDetail salaryPayRollDetail) {
		String paySlipId = salaryPayRollDetail.getPaySlipId();
		if (StringUtil.isEmpty(paySlipId)) {
			throw new BusinessException("获取主键失败");
		}

		salaryPayRollDetail.setModifyTime(new Date());
		salaryPayRollDetailMapper.updateByPrimaryKeySelective(salaryPayRollDetail);
	}

	/**
	 * 删除
	 *
	 * @param paySlipId
	 */
	public void delete(String paySlipId) {
		if (StringUtil.isEmpty(paySlipId)) {
			throw new BusinessException("获取主键失败");
		}
		salaryPayRollDetailMapper.deleteByPrimaryKey(paySlipId);
	}

	/**
	 * 员工工资批量生成
	 */
	public void batchCreateSalaryPayRollDetail(String payrollId) {
		Date date = new Date();
		
		SalaryPayRoll salaryPayRoll = salaryPayRollService.getById(payrollId);
		JSONArray  empQuotaArray = getEmpQuotaArray(salaryPayRoll.getPayrollYear(), salaryPayRoll.getPayrollMonth());
		
		JSONArray empArray = this.query("select  d.DEPT_NAME,e.*  from EMPLOYEE e LEFT JOIN SYS_DEPARTMENT d on e.DEPT_ID=d.DEPT_ID");
		for (int i = 0; i < empArray.size(); i++) {
			JSONObject emp = empArray.getJSONObject(i);
			String empId = emp.getString("empId");
			String empName = emp.getString("empName");
			String deptId = emp.getString("deptId");
			String deptName = emp.getString("deptName");
			// 员工基本工资项
			SalaryEmpBasic salaryEmpBasic = salaryEmpBasicService.getById(empId);
			if (salaryEmpBasic == null) {
				throw new BusinessException(empName + "基本工资项未配置");
			}
			double baseSalary = salaryEmpBasic.getBaseSalary();// 基本工资
			double workingAgeSubsidy = salaryEmpBasic.getWorkingAgeSubsidy();// 工龄工资
			double positionSalary = salaryEmpBasic.getPositionSalary();// 岗位工资
			double attendance = salaryEmpBasic.getAttendance();// 考勤
			double socialSecurity = salaryEmpBasic.getSocialSecurity();// 社保
			double deduction = 0.0;// 提成 计算员工工资定额
			String quotaJson = "";
			
			JSONObject empQuotaInfo = getEmpQuotaInfo(empId, empQuotaArray);
			if(empQuotaInfo!=null){
				deduction = empQuotaInfo.getDouble("empTotalQuota");
				quotaJson = empQuotaInfo.getString("quotaJson");
			}
			 
			double attendanceDeductions = 0.0;// 考勤扣款
			double tax = 0.0;// 个税

			// 实发工资=基本工资+工龄工资+岗位工资+考勤-社保
			double payableSalary = baseSalary + workingAgeSubsidy + positionSalary + attendance - socialSecurity;// 应发工资
			// 实发工资=应发工资+提成-考勤扣款-所得税
			double paidSalary = payableSalary + deduction - attendanceDeductions - tax;// 实发工资

			SalaryPayRollDetail salaryPayRollDetail = new SalaryPayRollDetail();
			salaryPayRollDetail.setPaySlipId(intIncrementId("SALARY_PAYROLL_DETAIL", "PAY_SLIP_ID"));
			salaryPayRollDetail.setPayrollId(payrollId);// 根据工资单而定
			salaryPayRollDetail.setEmpId(empId);
			salaryPayRollDetail.setEmpName(empName);
			salaryPayRollDetail.setDeptId(deptId);
			salaryPayRollDetail.setDeptName(deptName);
			salaryPayRollDetail.setBaseSalary(baseSalary);
			salaryPayRollDetail.setWorkingAgeSubsidy(workingAgeSubsidy);
			salaryPayRollDetail.setPositionSalary(positionSalary);
			salaryPayRollDetail.setDeduction(deduction);// 提成
			salaryPayRollDetail.setAttendance(attendance);
			salaryPayRollDetail.setAttendanceDeductions(attendanceDeductions);
			salaryPayRollDetail.setPayableSalary(payableSalary);
			salaryPayRollDetail.setSocialSecurity(salaryEmpBasic.getSocialSecurity());
			salaryPayRollDetail.setTax(tax);
			salaryPayRollDetail.setPaidSalary(paidSalary);
			salaryPayRollDetail.setQuotaJson(quotaJson);// 根据制作单而定
			// salaryPayRollDetail.setStatus(reqBean.getInfo("status"));
			salaryPayRollDetail.setOperator("");
			salaryPayRollDetail.setMakeTime(date);
			salaryPayRollDetail.setModifyTime(date);
			salaryPayRollDetail.setRemark("");
			salaryPayRollDetailMapper.insert(salaryPayRollDetail);
		}
	}
	
	
	public JSONObject getEmpQuotaInfo(String empId, JSONArray empQuotaArray) {
		JSONObject empQuotaInfo = null;
		for (int i = 0; i < empQuotaArray.size(); i++) {
			JSONObject json = empQuotaArray.getJSONObject(i);
			if (empId.equals(json.getString("empId"))) {
				empQuotaInfo = empQuotaArray.getJSONObject(i);
			}
		}
		return empQuotaInfo;
	}
	
	
	/**
	 * 获得个人工资定额明细、工作单明细
	 * @param empId
	 * @param quotaType
	 * @param payRollYear
	 * @param payRollMonth
	 * @return
	 */
	public JSONArray getEmpQuotaDetail(String empId, String quotaType, int payRollYear, int payRollMonth) {
		List<WorkBillItemQuotaDetail> workBillItemQuotaDetailList_all = getWorkBillItemQuotaDetail(payRollYear,
				payRollMonth);
		List<WorkBillItemQuotaDetail> workBillItemQuotaDetailList_part = new ArrayList<WorkBillItemQuotaDetail>();// 员工参与的工单明细

		for (WorkBillItemQuotaDetail workBillItemQuotaDetail : workBillItemQuotaDetailList_all) {
			String workBillItemType = workBillItemQuotaDetail.getWorkBillItemType();// 工单明细类型(1:边车上刊、2:全车上刊、3:胶贴制作)
			if (!StringUtil.isEmpty(quotaType)) {
				if (!quotaType.equals(workBillItemType)) {
					continue;
				}
			}
			List<EmpQuotaDetail> empQuotaDetailList = workBillItemQuotaDetail.getEmpQuotaDetails();
			// 筛选该员工参与的工单
			boolean flag = false;
			for (EmpQuotaDetail empQuotaDetail : empQuotaDetailList) {
				if (empId.equals(empQuotaDetail.getEmpId())) {
					flag = true;
				}
			}
			if (flag) {
				workBillItemQuotaDetailList_part.add(workBillItemQuotaDetail);
			}
		}

		JSONArray workBillDetailArray = new JSONArray();
		for (WorkBillItemQuotaDetail workBillItemQuotaDetail : workBillItemQuotaDetailList_part) {
			JSONObject workBillDetailJson = new JSONObject();
			double totalAmount = workBillItemQuotaDetail.getWorkBillItemAmount();
			String workBillItemId = workBillItemQuotaDetail.getWorkBillItemId();
			String workBillItemType = workBillItemQuotaDetail.getWorkBillItemType();
			OrderItem orderItem = orderService.getOrderItemByWorkBillItemId(workBillItemId);
			if (orderItem == null) {
				throw new BusinessException("制作单明细不存在!");
			}
			DecimalFormat df = new DecimalFormat("######0.00");
			workBillDetailJson.put("busPlateNo", orderItem.getBusPlateNo());// 车牌号
			workBillDetailJson.put("adName", orderItem.getAdName());// 广告内容
			workBillDetailJson.put("busType", orderItem.getBusType());// 车型
			workBillDetailJson.put("workBillItemId", workBillItemId);// 工作单明细id
			workBillDetailJson.put("totalAmount",df.format(totalAmount));// 工作单总定额
			workBillDetailJson.put("workBillItemType", workBillItemType);// 工单明细类型(1:边车上刊、2:全车上刊、3:胶贴制作)
			List<EmpQuotaDetail> empQuotaDetails = workBillItemQuotaDetail.getEmpQuotaDetails();
			double totalQuota = 0.0;
			for (EmpQuotaDetail empQuotaDetail : empQuotaDetails) {
				totalQuota += empQuotaDetail.getEmpQuota();
			}
			JSONArray array_x = new JSONArray();
			for (EmpQuotaDetail empQuotaDetail : empQuotaDetails) {
				JSONObject json = new JSONObject();
				String empId_x = empQuotaDetail.getEmpId();
				String empName = empQuotaDetail.getEmpName();

				double empQuota = empQuotaDetail.getEmpQuota();
				double empAmount = (empQuota / totalQuota) * totalAmount;// 人员完成工作单明细所得金额
				json.put("empId", empId_x);
				json.put("empName", empName);
				json.put("empAmount",df.format(empAmount));
				array_x.add(json);
				if (empId.equals(empId_x)) {
					workBillDetailJson.put("myEmpId", empId_x);
					workBillDetailJson.put("empName", empName);
					workBillDetailJson.put("myQuota", df.format(empAmount));
				}
			}
			workBillDetailJson.put("empArray", array_x);
			workBillDetailArray.add(workBillDetailJson);
		}
		return workBillDetailArray;
	}
	
	
	/**
	 * 获取员工当月总定额工资
	 * @param payrollYear
	 * @param payrollMonth
	 * @return
	 */
	public JSONArray getEmpQuotaArray(int payrollYear, int payrollMonth) {
		List<WorkBillItemQuotaDetail> workBillItemQuotaDetailList = getWorkBillItemQuotaDetail(payrollYear,payrollMonth);
		Map<String, Map<String, Double>> empQuotaTypeMaps = new HashMap<String, Map<String, Double>>();
		for (WorkBillItemQuotaDetail workBillItemQuotaDetail : workBillItemQuotaDetailList) {
			double amount = workBillItemQuotaDetail.getWorkBillItemAmount();
			String workBillItemType = workBillItemQuotaDetail.getWorkBillItemType();// 工单明细类型(1:边车上刊、2:全车上刊、3:胶贴制作)
			List<EmpQuotaDetail> empQuotaDetails = workBillItemQuotaDetail.getEmpQuotaDetails();
			double totalQuota = 0.0;
			for (EmpQuotaDetail empQuotaDetail : empQuotaDetails) {
				totalQuota += empQuotaDetail.getEmpQuota();
			}
			for (EmpQuotaDetail empQuotaDetail : empQuotaDetails) {
				String empId = empQuotaDetail.getEmpId();
				double empQuota = empQuotaDetail.getEmpQuota();
				double empAmount = (empQuota / totalQuota) * amount;// 人员完成工作单明细所得金额
				if (empQuotaTypeMaps.containsKey(empId)) {
					Map<String, Double> quotaTypeMaps = empQuotaTypeMaps.get(empId);// 定额类型：定额类型总和
					if (quotaTypeMaps.containsKey(workBillItemType)) {
						empAmount += quotaTypeMaps.get(workBillItemType);// 累加
						quotaTypeMaps.put(workBillItemType, empAmount);
					} else {
						quotaTypeMaps.put(workBillItemType, empAmount);
					}
					empQuotaTypeMaps.put(empId, quotaTypeMaps);
				} else {
					Map<String, Double> quotaTypeMaps = new HashMap<String, Double>();// 定额类型：定额类型总和
					quotaTypeMaps.put(workBillItemType, empAmount);
					empQuotaTypeMaps.put(empId, quotaTypeMaps);
				}
			}
		}

		JSONArray empArray = new JSONArray();
		for (Map.Entry<String, Map<String, Double>> entry : empQuotaTypeMaps.entrySet()) {
			JSONObject empJson = new JSONObject();
			String empId = entry.getKey();
			empJson.put("empId", empId);
			double empTotalQuota = 0.0;// 个人总定额
			
			JSONArray quotaTypeArray = new JSONArray();
			Map<String, Double> quotaTypeMaps = entry.getValue();
			for (Map.Entry<String, Double> entry_x : quotaTypeMaps.entrySet()) {
				JSONObject quotaTypeJson = new JSONObject();
				String quotaType = entry_x.getKey();
				double empQuota = entry_x.getValue();
				quotaTypeJson.put("quotaType", quotaType);
				QuotaConfig quotaConfig = quotaConfigService.getById(quotaType);
				quotaTypeJson.put("quotaTypeName", quotaConfig.getQuotaConfigName());
				quotaTypeJson.put("empQuota", empQuota);
				quotaTypeArray.add(quotaTypeJson);
				empTotalQuota += empQuota;
			}
			empJson.put("empTotalQuota", empTotalQuota);// 员工总定额
			empJson.put("quotaJson", quotaTypeArray);
			empArray.add(empJson);
		}
		return empArray;
	}

	/**
	 * 获得工作单明细定额信息
	 * @param payrollYear
	 * @param payrollMonth
	 * @return
	 */
	public List<WorkBillItemQuotaDetail> getWorkBillItemQuotaDetail(int payRollYear,int payRollMonth) {
		
		List<WorkBillItemQuotaDetail> workBillItemQuotaDetailList = new ArrayList<>();
		// 获取已完工的制作单对应的工单明细
		JSONArray workBillItemArray = getWorkBillItemInfo(payRollYear, payRollMonth);
		Map<String,String> workBillIdMaps = new HashMap<>();
		for (int i = 0; i < workBillItemArray.size(); i++) {
			JSONObject workBIllItemJson = workBillItemArray.getJSONObject(i);

			String workBillItemId = workBIllItemJson.getString("workbillItemId");// 工单明细id
			if(workBillIdMaps.containsKey(workBillItemId)){
				continue;// 胶贴制作一个工作单明细对应多个任务（裁图、打印、覆膜）
			}else {
				workBillIdMaps.put(workBillItemId, workBillItemId);
				String workBillType = workBIllItemJson.getString("workbillType");// 胶贴制作、上刊
				double adQrea = workBIllItemJson.getDouble("adArea");// 面积

				double quota = 0.0;
				WorkBillItemQuotaDetail workBillItemQuotaDetail = new WorkBillItemQuotaDetail();
				List<EmpQuotaDetail> empQuotaDetails = new ArrayList<>();
				if (BusConstant.WorkBillType.WORK_BILL_TYPE_PUBLISH.equals(workBillType)) {// 广告上刊工单
					String publishType = workBIllItemJson.getString("publishType");// 发布形式
					String makeType = workBIllItemJson.getString("makeType");// 制作形式
					QuotaConfig quotaConfig = quotaConfigService.getByPublishTypeAndMakeType(publishType, makeType);
					if(quotaConfig == null){
						throw new BusinessException("未找到对应的定额配置");
					}
					quota = quotaConfig.getQuota();
					// 需要通过工作组找到完成工作单明细的人员
					String workGroupId = workBIllItemJson.getString("workGroupId");// 工作组id
					JSONArray memberArray = employeeGroupService.getGroupMemberByGroupId(workGroupId);
					
					String quotaConfigType = quotaConfigService.getByPublishType(publishType);
					workBillItemQuotaDetail.setWorkBillItemType(quotaConfigType);// 工单明细类型(1:边车上刊、2:全车上刊、3:胶贴制作)
					
					for (int j = 0; j < memberArray.size(); j++) {
						JSONObject empJson = memberArray.getJSONObject(j);
						String empId = empJson.getString("empId");
						empQuotaDetails.add(getEmpQuotaDetailByEmpId(empId, quotaConfigType));
					}
				} else {// 胶贴制作工单
					String quotaConfigType = BusConstant.QUOTA_CONFIG_TYPE.QUOTA_CONFIG_TYPE_MAKE;// 胶贴制作
					QuotaConfig quotaConfig = quotaConfigService.getByQuotaConfigParentId(quotaConfigType);// todo  需要优化
					workBillItemQuotaDetail.setWorkBillItemType(quotaConfigType);// 工单明细类型(1:边车上刊、2:全车上刊、3:胶贴制作)
					quota = quotaConfig.getQuota();
					
					// 需要通过AD_ORDER_ITEM找到完成工作单明细的人员
					String caituEmpId = workBIllItemJson.getString("caituEmpId");// 裁图人员id
					empQuotaDetails.add(getEmpQuotaDetailByEmpId(caituEmpId, quotaConfigType));
					String printEmpId = workBIllItemJson.getString("printEmpId");// 打印人员id
					empQuotaDetails.add(getEmpQuotaDetailByEmpId(printEmpId, quotaConfigType));
					String fumoEmpId = workBIllItemJson.getString("fumoEmpId");// 覆膜id
					empQuotaDetails.add(getEmpQuotaDetailByEmpId(fumoEmpId, quotaConfigType));
				}
				
				workBillItemQuotaDetail.setWorkBillItemId(workBillItemId);// 工作单id
				workBillItemQuotaDetail.setWorkBillItemAmount(adQrea * quota);// 工作单总定额
				workBillItemQuotaDetail.setEmpQuotaDetails(empQuotaDetails);// 工作单明细对应的人员信息
				workBillItemQuotaDetailList.add(workBillItemQuotaDetail);
			}
		}
		return workBillItemQuotaDetailList;
	}
	
	/**
	 * 获得工单明细、制作单明细信息
	 * @param payrollYear
	 * @param payrollMonth
	 * @return
	 */
	public JSONArray getWorkBillItemInfo(int payRollYear,int payRollMonth){
		int mount_start = payRollMonth;
		int mount_end = payRollMonth + 1;
		String payRollMonth_start = mount_start > 10 ? String.valueOf(mount_start) : "0" + mount_start;
		String payRollMonth_end = mount_end > 10 ? String.valueOf(mount_end) : "0" + mount_end;
		String dateCondition_start = payRollYear + "-" + payRollMonth_start;
		String dateCondition_end = payRollYear + "-" + payRollMonth_end;
		
		// 获取已完工的制作单对应的工单明细
		StringBuffer sql = new StringBuffer("select o.ORDER_ID,o.`STATUS`,awi.WORKBILL_ID,awi.WORKBILL_ITEM_ID,aw.C_WORKBILL_TYPE,");
		sql.append(" aoi.ORDER_ITEM_ID,aoi.C_PUBLISH_TYPE,aoi.C_MAKE_TYPE,aoi.AD_AREA,awt.WORK_GROUP_ID,aoi.CAITU_EMP_ID,aoi.PRINT_EMP_ID,aoi.FUMO_EMP_ID");
		sql.append(" FROM AD_WORKBILL_ITEM awi");
		sql.append(" LEFT JOIN AD_ORDER_ITEM aoi ON aoi.ORDER_ITEM_ID = awi.ORDER_ITEM_ID");
		sql.append(" LEFT JOIN AD_WORKBILL aw ON aw.WORKBILL_ID = awi.WORKBILL_ID");
		sql.append(" LEFT JOIN AD_ORDER o ON o.ORDER_ID = aw.ORDER_ID");
		sql.append(" LEFT JOIN AD_WORKBILL_TASK awt on awt.WORKBILL_ITEM_ID = awi.WORKBILL_ITEM_ID");
		sql.append(" where o.`STATUS`= ? and o.FINISH_TIME > ? and o.FINISH_TIME< ?");
		return query(sql.toString(), BusConstant.OrderStatus.FINISHED,dateCondition_start,dateCondition_end);
	}
	

	/**
	 * 通过员工id和定额类型找打对应的定额值
	 * 
	 * @param empId
	 * @param quotaConfigType
	 * @return
	 */
	public EmpQuotaDetail getEmpQuotaDetailByEmpId(String empId, String quotaConfigType) {
		EmpQuotaDetail empQuotaDetail = new WorkBillItemQuotaDetail().new EmpQuotaDetail();
		Employee employee = employeeService.getById(empId);
		SalaryEmpQuota salaryEmpQuota = salaryEmpQuotaService.getById(empId, quotaConfigType);
		empQuotaDetail.setEmpId(empId);
		empQuotaDetail.setEmpName(employee.getName());
		empQuotaDetail.setEmpQuota(salaryEmpQuota.getEmpQuota());
		return empQuotaDetail;
	}

	/**
	 * 员工收入分析
	 * 
	 * @param empId
	 * @param year
	 * @return
	 */
	public JSONObject analyzeEmpSalary(String empId, String payRollYear) {
		JSONObject resultJson = new JSONObject();
		SqlBuilder sql = new SqlBuilder("select sp.PAYROLL_YEAR,sp.PAYROLL_MONTH,spd.PAID_SALARY,spd.DEDUCTION from SALARY_PAYROLL_DETAIL spd ");
		sql.appendSql("LEFT JOIN SALARY_PAYROLL sp on spd.PAYROLL_ID=sp.PAYROLL_ID where sp.PAYROLL_YEAR=? and spd.EMP_ID=? ORDER BY sp.PAYROLL_MONTH",
				payRollYear, empId);
		JSONArray array = this.query(sql);
		double[] paidSalaryArray = new double[12];
		double[] deductionArray = new double[12];
		for (int i = 0; i < array.size(); i++) {
			JSONObject json = array.getJSONObject(i);
			int payrollMonth = json.getInt("payrollMonth");// 月份
			double paidSalary = json.getDouble("paidSalary");
			double deduction = json.getDouble("deduction");
			paidSalaryArray[payrollMonth - 1] = paidSalary;
			deductionArray[payrollMonth - 1] = deduction;
		}
		resultJson.put("paidSalaryArray", paidSalaryArray);
		resultJson.put("deductionArray", deductionArray);
		return resultJson;
	}

	public JSONObject getEmpByEmpName(String empName) {
		JSONObject resultData = new JSONObject();
		SqlBuilder sql = new SqlBuilder(
				"select e.EMP_NAME,e.EMP_ID,d.DEPT_NAME  from EMPLOYEE e LEFT JOIN SYS_DEPARTMENT d on e.DEPT_ID=d.DEPT_ID where e.EMP_NAME like '%" + empName + "%' ");
		JSONArray array = this.query(sql);
		resultData.put("empArray", array);
		resultData.put("resultCode", "1");
		resultData.put("empName", empName);
		return resultData;
	}

}